package com.example.controller;

import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.example.domain.RoleMenu;
import com.example.result.Result;
import com.example.service.RoleMenuService;
import com.example.tool.GsonTool;

/**
 * @author 袁小弘
 * @describe 角色菜单控制层
 * @date 2020-02-21
 */
@RestController
@RequestMapping("/api")
public class RoleMenuController {

    @Autowired
    RoleMenuService roleMenuService;

    // ===============================角色菜单管理==============================================

    /**
     * @param json
     * @return 添加成功则提示成功信息，反之则提示失败信息
     * @describe 添加角色菜单信息
     */
    @RequestMapping(value = "/rolemenus", method = RequestMethod.POST)
    public Result addRoleMenu(@RequestBody String json) {

        return roleMenuService.addRoleMenu(json);

    }

    /**
     * @param json
     * @return 更新成功则提示成功信息，反之则提示失败信息
     * @describe 更新角色菜单信息
     */
    @RequestMapping(value = "/rolemenus", method = RequestMethod.PUT)
    public Result updateRoleMenu(@RequestBody String json) {
        // 将JSON数据转换为RoleMenu对象实体
        JSONObject parseObject = JSON.parseObject(json);
        RoleMenu roleMenu = GsonTool.getGsonInstance().fromJson(parseObject.getString("json"), RoleMenu.class);
        return roleMenuService.updateRoleMenu(roleMenu);

    }

    /**
     * @param roleMenuId
     * @return 删除成功则提示成功信息，反之则提示失败信息
     * @describe 根据角色菜单编号删除角色菜单信息
     */
    @RequestMapping(value = "/rolemenus/{roleMenuId}", method = RequestMethod.DELETE)
    public Result deleteRoleMenuById(@PathVariable("roleMenuId") String roleMenuId) {
        return roleMenuService.deleteRoleMenuById(roleMenuId);

    }

    /**
     * @return 以JSON格式返回所有班级信息
     * @describe 查询所有角色菜单信息
     */
    @RequestMapping(value = "/rolemenus", method = RequestMethod.GET)
    public Result queryAllRoleMenu() {
        return roleMenuService.queryAllRoleMenu();

    }

    /**
     * @param roleMenuId
     * @return 以JSON格式返回该角色菜单信息
     * @describe 根据编号查询角色菜单信息
     */
    @RequestMapping(value = "/rolemenus/{roleMenuId}", method = RequestMethod.GET)
    public Result queryRoleMenuById(@PathVariable("roleMenuId") String roleMenuId) {
        return roleMenuService.queryRoleMenuById(roleMenuId);

    }

    /**
     * @param json
     * @return 添加成功则返回一个成功提示，反之则提示失败信息
     * @describe 通过Excel批量添加角色菜单信息
     */
    @RequestMapping(value = "/rolemenus/excels", method = RequestMethod.POST)
    public Result addRoleMenuByExcel(@RequestBody String json) {

        JSONObject parseObject = JSON.parseObject(json);
        List<RoleMenu> list = JSON.parseArray(parseObject.getString("json"), RoleMenu.class);

        return roleMenuService.addRoleMenuByExcel(list);

    }

    /**
     * @param Ids
     * @return 删除成功则返回一个成功提示，反之则提示失败信息
     * @describe 根据角色菜单编号批量删除角色菜单信息
     */
    @RequestMapping(value = "/rolemenus/dels", method = RequestMethod.POST)
    public Result deleteRoleMenuByIds(@RequestBody Map<String, String> Ids) {

        return roleMenuService.deleteRoleMenuByIds(Ids);
    }

    /**
     * @param json
     * @return 查询成功则返回一个成功提示，反之则提示失败信息
     * @describe 角色菜单信息模糊查询
     */
    @RequestMapping(value = "/rolemenus/serachs", method = RequestMethod.POST)
    public Result serachByInfo(@RequestBody String json) {
        System.out.println(json);
        JSONObject parseObject = JSON.parseObject(json);
        String[] info = parseObject.getString("json").split(",");

        return roleMenuService.serachByInfo(info);

    }

    /**
     * @return 查询成功则返回一个成功提示，反之则提示失败信息
     * @describe 查询所有未分配的角色名称
     */
    @RequestMapping(value = "/rolemenus/roleNames", method = RequestMethod.GET)
    public Result queryRoleNameNotAllocation() {
        return roleMenuService.queryRoleNameNotAllocation();
    }

    /**
     * @return 查询成功则返回一个成功提示，反之则提示失败信息
     * @describe 查询所有角色权限已分配信息
     */
    @RequestMapping(value = "/rolemenus/roleAndMenus", method = RequestMethod.GET)
    public Result queryAllRoleAndMenuInfo() {
        return roleMenuService.queryAllRoleAndMenuInfo();
    }
}
